/*
 * create by lixy 20150323
 * Just for DMap(tuteng) Project
 * copyright@ 2015
 */
"use strict";
/* Controllers */
app.controller("loginController",  ['$scope', '$location', '$rootScope', '$routeParams', 'loginService', function ($scope, $location, $rootScope, $routeParams, loginService) {

    var vm = $scope.vm = {};
    vm.p = {};
    $scope.isInvalid = false;
    vm.p.redirectURL = $location.search().redirectURL;

    vm.FOCUS_COLOR = "#FF552E";
    $scope.mobileLogin = function(type) {
        var vm = $scope.vm;
        if(type==1) {
            vm.isMobile = true;
            vm.color1 = vm.FOCUS_COLOR;
            vm.color2 = "";
        }else {
            vm.isMobile = false;
            vm.color1 = "";
            vm.color2 = vm.FOCUS_COLOR;
        }
    };
    $scope.mobileLogin(0);

    $scope.lostPassword = function() {
        alert("请手机登陆后，'个人中心' -> '我的信息' 页面下进行 '修改密码'.");
    };

    var errorFn = function (result) {
        alert(result);
    };

    $scope.changeVerifyCode = function() {
        var imgEle = document.getElementById("imgVerifyCode");
        imgEle.src="/getVerifyCode?a="+ new Date().getTime();
    };

    $scope.loginAccountCode = function () {
        var vm = $scope.vm;
        var validateCode = function (e) {
            var codePattern = /^[A-Za-z0-9]{4}$/g;
            return codePattern.test(e)
        };
        if(!validateCode(vm.p.verifyCode)) {
            alert("验证码无效.");
            return;
        }
        if(vm.p.username.length < 2 || vm.p.password.length < 4) {
            alert("用户名或密码无效.");
            return;
        }
        var params = {
            verifyCode: vm.p.verifyCode,
            username: vm.p.username,
            password: vm.p.password,
            redirectURL: vm.p.redirectURL
        };
        var procFn = function (response) {
            if (response.success && response.data) {
                if(vm.p.password=="000000") {
                    alert("认证成功！请尽快到'个人中心'->'我的信息'修改密码！");
                }
                window.location = vm.p.redirectURL;
            } else {
                alert(response.message);
                $scope.changeVerifyCode();
            }
        };
        loginService.loginAccountCode(params, procFn, errorFn);
    };

    var countdown=60;
    $scope.sendDynamicCode = function() {
        var validateMobile = function (e) {
            var mobilePattern = /^1[23456789]\d{9}$/g;
            return mobilePattern.test(e)
        };
        if(!validateMobile(vm.p.phoneNumber)) {
            alert("手机号码无效.");
            return;
        }

        var obj = $("#btn");
        var setTime = function (obj) { //发送验证码倒计时
            if (countdown == 0) {
                obj.attr('disabled',false);
                obj.val("发送动态密码");
                countdown = 60;
                return;
            } else {
                obj.attr('disabled',true);
                obj.val("重新发送(" + countdown + ")");
                countdown--;
            }
            setTimeout(function(){setTime(obj)} ,1000);
        };
        setTime(obj);

        var params = {
            phoneNumber: vm.p.phoneNumber
        };
        var procFn = function (response) {
            if (response.success && response.data) {
                alert("短信已发送！")
            } else {
                alert(response.message);
            }
        };
        loginService.sendDynamicCode(params, procFn, errorFn);
    };

    $scope.loginAjaxDynamicCode = function () {
        var vm = $scope.vm;
        var validateMobile = function (e) {
            var mobilePattern = /^1[23456789]\d{9}$/g;
            return mobilePattern.test(e)
        };
        var validateCode = function (e) {
            var codePattern = /^[A-Za-z0-9]{6}$/g;
            return codePattern.test(e)
        };
        if(!validateMobile(vm.p.phoneNumber) || !validateCode(vm.p.dynamicCode)) {
            alert("动态密码无效.");
            return;
        }
        var params = {
            dynamicCode: vm.p.dynamicCode,
            phoneNumber: vm.p.phoneNumber,
            redirectURL: vm.p.redirectURL
        };
        var procFn = function (response) {
            if (response.success && response.data) {
                //alert("认证成功！");
                window.location = vm.p.redirectURL;
            } else {
                alert(response.message);
            }
        };
        loginService.loginAjaxDynamicCode(params, procFn, errorFn);
    };
}]);

/* Services */
app.service('loginService',['ajaxService', function (ajaxService) {

    // /sendDynamicCode?phoneNumber=" + n.MobileValue;
    this.sendDynamicCode = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "./sendDynamicCode2", successFunction, errorFunction);
    };

    this.loginAjaxDynamicCode = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "./loginAjaxDynamicCode2", successFunction, errorFunction);
    };

    this.loginAccountCode = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "./loginAccountCode", successFunction, errorFunction);
    };
}]);

